package com.wthink.newMain.settings.config.inner.basic

import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import com.wthink.hzgj.BusApplication
import com.wthink.hzgj.data.Result
import com.wthink.hzgj.service.DevService
import com.wthink.hzgj.util.toast
import com.wthink.newMain.data.BasicInfo
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext

class BasicViewmodel : ViewModel(){

    val basicInfo = MutableLiveData<BasicInfo>()

    val colorsItems = arrayListOf( "蓝色", "黄色", "黑色", "白色", "绿色", "黄绿色", "其他", "未知")

    val vdr_model = MutableLiveData("")
    val vdr_vendorid = MutableLiveData("")
    val vid = MutableLiveData("")
    val simno = MutableLiveData("")
    val car_num = MutableLiveData("")
    val province_id = MutableLiveData("")
    val city_id = MutableLiveData("")
    val car_vin = MutableLiveData("")
    val sn = MutableLiveData("")
    val dev_id = MutableLiveData("")
    val car_id = MutableLiveData("")
    val line_id = MutableLiveData("")
    val driver_id = MutableLiveData("")
    val driver_name = MutableLiveData("")

    fun config(basicConfig: BasicInfo){
        viewModelScope.launch(Dispatchers.IO) {
            val netconfigSer = DevService.setBusconfig(basicConfig)
            if (netconfigSer is Result.Success) {
                withContext(Dispatchers.Main) {
                    toast(BusApplication.busContext, "配置成功")
                }
            } else if (netconfigSer is Result.Error) {
                withContext(Dispatchers.Main) {
                    toast(BusApplication.busContext, netconfigSer.exception.toString())
                }
            }
        }
    }


    fun queryBusInfo(){
        viewModelScope.launch(Dispatchers.IO) {
            val busInfo = DevService.queryBasic()
            if (busInfo is Result.Success) {
                basicInfo.postValue(busInfo.data)
            }else if (busInfo is Result.Error) {
                withContext(Dispatchers.Main) {
                    toast(BusApplication.busContext, busInfo.exception.toString())
                }
            }
        }
    }
}